import {
  getMoniteSessions, getMoniteStudentsInfo, onStartScore, onFinishScore, getSubjectItems
} from '@/api/kcjk/index'


export default {
  name: 'ksgl-jbxx',
  data() {
    return {
      tableData: [],
      listLoading: false,
      formVisible: false,
      fromData: [],
      lie: 0,
      getRow: [],
      ccdm: '',
      ccztdm: 0,
      ccmc: '',
      hengNumber: 0,
      subjectItems: []
    }
  },
  created() {
    this.initData()
    this.getSubject()
  },
  methods: {
    getSubject () {
      getSubjectItems().then((res) => {
        console.log(res, '123')
        this.subjectItems = res.Response
      })
    },
    getRGB (str) {
      // str = 55295
      // console.log(str.toString(16))
      str = Math.max(0, Math.min(str, 16777215))
      let hexString = str.toString(16)
      while (hexString.length < 6) {
        hexString = '0' + hexString
      }
      return hexString
    },
    goPrint() {
      this.$router.push({
        path: "/print/index?ccdm=" + this.ccdm + '&ccmc=' + this.ccmc
      });
    },
    search() {
      this.initData()
      this.fromData = []
      this.getRow = []
    },
    reset() {
      this.search()
    },
    initData() {
      // this.listLoading = true
      getMoniteSessions().then(res => {
        this.tableData = res.Response
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    getRowDetails (ccdm) {

      getMoniteStudentsInfo({ccdm: ccdm}).then(res => {
        let data = res.Response
        let arr = []
        for (let i = 0; i < data.length; i ++) {
          if (data[i].lie > this.lie) {
            this.lie = data[i].lie
          }
        }
        this.getCelList(data, arr)
        // 先对行进行排列
        // 好叻，避免不了要对行进行排序了。 特么的
        for (let i = 0; i< arr.length; i++) {
          for (let j = 0; j< i; j++) {
            if (parseInt(arr[i].hang) < parseInt(arr[j].hang)) {
              let a = arr[i]
              arr[i] = arr[j]
              arr[j] = a
            }
          }
        }
        this.fromData = Object.assign([], arr)
        // 好了，行就不管了，直接对里面的列冒个泡
        this.fromData = this.fromData.map(item => {
          for (let i = 0; i< item.nodes.length; i++) {
            for (let j = 0; j< i; j++) {
              if (parseInt(item.nodes[i].lie) < parseInt(item.nodes[j].lie)) {
                let a = item.nodes[i]
                item.nodes[i] = item.nodes[j]
                item.nodes[j] = a
              }
            }
          }
          return item
        })
        // console.log(this.fromData)
        // delete this.fromData[0].nodes[1]
        // this.fromData[0].nodes.splice(1, 1)
        console.log(this.fromData, 'delete')
      })
    },
    resetRwo () {
      if (this.ccdm) {
        this.getRowDetails (this.ccdm)
      }
    },
    getRowStudentsInfo (row) {
      this.getRow = Object.assign(this.getRow, [row])
      this.ccztdm = row.ccztdm
      this.ccdm = row.ccdm
      this.ccmc = row.ccmc
      this.lie = 0
      this.getRowDetails (row.ccdm)
    },
    getNotRow (p, data) {
      // if (data.nodes)
      let a = false
      for (let i = 0; i < data.nodes.length; i ++) {
        if (data.nodes[i].lie === p) {
          a = true
        }
      }
      return a
    },
    getCelList (data, list) {
      for (let i = 0; i < data.length; i++) {
        let bl = false
        // console.log(data, list)
        if (list.length === 0) {
          bl = true
          list.push(
            {
              hang: data[i].hang,
              nodes:  [data[i]]
            }
          )
        } else {
          for (let j = 0; j < list.length; j++) {
            if (list[j].hang === data[i].hang) {
              if (list[j].nodes) {
                bl = true
                list[j].nodes.push(data[i])
              }
            }
          }
        }
        if (!bl) {
          list.push(
            {
              hang: data[i].hang,
              nodes:  [data[i]]
            }
          )
        }
      }
    },
    handleFinishEdit () {
      if (this.getRow.length > 0) {
        onFinishScore(this.getRow[0]).then(res => {
          this.initData()
          this.getRowStudentsInfo(this.getRow[0])
        })
      } else {
        this.$message({
          message: '请选择题目进行操作',
          type: 'error',
          duration: 1500
        })
      }
    },
    handleAddEdit() {
      if (this.getRow.length > 0) {
        onStartScore(this.getRow[0]).then(res => {
          this.initData()
          this.getRowStudentsInfo(this.getRow[0])
        })
      } else {
        this.$message({
          message: '请选择题目进行评分',
          type: 'error',
          duration: 1500
        })
      }
    },
    removeForm(isRefresh) {
      this.formVisible = false
      if (isRefresh) {
        this.initData()
      }
    }
  }
}
